package com.jiezheng.huinong.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jiezheng.huinong.dao.IndexManageDAOImpl;
import com.jiezheng.huinong.model.IndexInfo;
import com.jiezheng.huinong.model.OperationStatus;
import com.jiezheng.huinong.util.JqPage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class IndexInfoAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 4904843678076641155L;
	private JqPage pageInfo = new JqPage();
	private List<IndexInfo> indexes = new ArrayList<IndexInfo>();
	private String sord = "";
	private String sidx = "";

	// 查询条件
	private IndexInfo cond = new IndexInfo();

	/**
	 * 列出所有指标
	 * 
	 * @return
	 */
	public String listIndexInfo() {

		// 检验部门 相关性
		cond.setDeptNo(deptNoCondition());
		cond.setStatus(OperationStatus.DEFAULT);
		budgetUnitCondition();
		payMethodCondition();

		Integer total = (new IndexManageDAOImpl()).getTotalIndexNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		indexes = (new IndexManageDAOImpl()).getIndexInfo(cond, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}

	/**
	 * 列出所有已初审指标
	 * 
	 * @return
	 */
	public String auditIndexInfo() {

		// 检验部门 相关性
		cond.setDeptNo(deptNoCondition());
		cond.setStatus(OperationStatus.AUDITED);
		budgetUnitCondition();
		payMethodCondition();
		
		Integer total = (new IndexManageDAOImpl()).getTotalIndexNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		indexes = (new IndexManageDAOImpl()).getIndexInfo(cond, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}
	
	/**
	 * 列出所有已终审指标
	 * 
	 * @return
	 */
	public String finalAuditIndexInfo() {

		// 检验部门 相关性
		cond.setDeptNo(deptNoCondition());
		cond.setStatus(OperationStatus.FINAL_AUDITED);
		budgetUnitCondition();
		payMethodCondition();

		Integer total = (new IndexManageDAOImpl()).getTotalIndexNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		indexes = (new IndexManageDAOImpl()).getIndexInfo(cond, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}

	/**
	 * 列出所有未审核指标
	 * 
	 * @return
	 */
	public String noAuditIndexInfo() {

		// 检验部门 相关性
		cond.setDeptNo(deptNoCondition());
		cond.setStatus(OperationStatus.NO_AUDIT);
		budgetUnitCondition();
		payMethodCondition();

		Integer total = (new IndexManageDAOImpl()).getTotalIndexNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		indexes = (new IndexManageDAOImpl()).getIndexInfo(cond, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}

	/**
	 * 列出所有待送审指标
	 * 
	 * @return
	 */
	public String toAuditIndexInfo() {

		// 检验部门 相关性
		cond.setDeptNo(deptNoCondition());
		budgetUnitCondition();

		Integer total = (new IndexManageDAOImpl())
				.getTotalToAuditIndexNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		indexes = (new IndexManageDAOImpl()).getToAuditIndexInfo(cond,
				startRow, pageInfo.getRows());
		return SUCCESS;
	}

	/**
	 * 列出所有待挂接指标
	 * 
	 * @return
	 */
	public String listJointIndexInfo() {

		Integer total = (new IndexManageDAOImpl()).getTotalJointIndexNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		indexes = (new IndexManageDAOImpl()).getJointIndexInfo(cond, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}

	private String deptNoCondition() {

		// 检验当前用户的部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");

		// 财政所用户处理应可以看到相关的指标
		if (deptNo.startsWith("2")) {
			deptNo = "";
		}

		// 国库股/中国人民银行应可以看到所有指标
		else if (deptNo.equals("003") || deptNo.equals("800") || deptNo.equals("009")) {
			deptNo = "";
		}
		return deptNo;
	}
	
	private void budgetUnitCondition(){
		
		// 检验当前用户的部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String budgetUnit = (String) session.get("deptNo");

		// 财政所用户处理应可以看到相关的指标
		if (budgetUnit.startsWith("2")) {
			budgetUnit = String.format("%s001", budgetUnit);
			cond.setBudgetUnit(budgetUnit);
		}
		
		// 其它部门处理这个字段
	}
	
	private void payMethodCondition(){
		
		// 检验当前用户的部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");
		
		// 实拨部门只能看到可以看到实拨相关的指标
		if (deptNo.equals("009")) {
			cond.setPayMethod("3");
		}
		
	}

	public JqPage getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(JqPage pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<IndexInfo> getIndexes() {
		return indexes;
	}

	public void setIndexes(List<IndexInfo> indexes) {
		this.indexes = indexes;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public IndexInfo getCond() {
		return cond;
	}

	public void setCond(IndexInfo cond) {
		this.cond = cond;
	}

}
